// pages/qrcode/qrcode.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcode:'iVBORw0KGgoAAAANSUhEUgAAAIQAAACEAQMAAABrihHkAAAABlBMVEX///8AAABVwtN+AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAyUlEQVRIie2UwQ3DMAwDtQH331IbqCSdFM2nRcw+IxiGdQ+DoiVXPfEjZqZRAx8C0lpD4kNAprXgPSQYzD8IpSEmXKBxfan9PpHhjstb3Cemb3X7hBlX0bJD4Cah7+wmk7P2LaKnA433GwSEOQHcEBWQUjdxk9JOCDPYL1RCeNTEqc5JCKeEykZCj0r3SK8EbMxKyEklswOiAVHa+k0C0qsZZFtFZA2u9CEl9Ev3nhO3Tfyb4FPhBim3FDx4AfGtMn+N7zZ54ku8AGxKJ5rKth5zAAAAAElFTkSuQmCC',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  onsaveqrcode(e){
      wx.getSetting({ //获取用户的当前设置
        success: (res) => {
          console.log(res);
          if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
            this.saveImageToPhotosAlbum();
          } else {
            wx.authorize({ //如果没有授权，向用户发起请求
              scope: 'scope.writePhotosAlbum',
              success: () => {
                this.saveImageToPhotosAlbum();
              },
              fail: () => {
                wx.openSetting({
                  success(settingdata) {
                    console.log("settingdata", settingdata)
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      wx.showModal({
                        title: '提示',
                        content: '获取权限成功,再次点击图片即可保存',
                        showCancel: false,
                      })
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: '获取权限失败，将无法保存到相册哦~',
                        showCancel: false,
                      })
                    }
                  },
                })
              }
            })
          }
        }
      })

  },

  

  saveImageToPhotosAlbum() {
    let qrcode = this.data.qrcode;
    let base64 = qrcode.replace(/^data:image\/\w+;base64,/, ""); //去掉data:image/png;base64,
    let filePath = wx.env.USER_DATA_PATH + '/hym_pay_qrcode.png';
    wx.getFileSystemManager().writeFile({
      filePath: filePath, //创建一个临时文件名
      data: base64, //写入的文本或二进制数据
      encoding: 'base64', //写入当前文件的字符编码
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: filePath,
          success: function(res2) {
            wx.showToast({
              title: '保存成功，请从相册选择再分享',
              icon: "none",
              duration: 5000
            })
          },
          fail: function(err) {
            // console.log(err.errMsg);
          }
        })
      },
      fail: err => {
        //console.log(err)
      }
    })
  },


      // wx.downloadFile({
    //   url: 'http://mmbiz.qpic.cn/mmbiz_png/ThrgO9y4NFbGxSQOk36wPfwGyLTlekRtjuyOwY2hgljxlsfZvrsR8Ms9hI9Sl4jseJlNbzxgH0Xia2n6G8nuRWg/0?wx_fmt=png',//图片地址
    //   success: function (res) {
    //     //图片保存到本地
    //     wx.saveImageToPhotosAlbum({
    //       filePath: res.tempFilePath,
    //       success: function (data) {
    //         wx.hideLoading()
    //         wx.showModal({
    //           title: '提示',
    //           content: '您的二维码已保存到相册，赶快识别二维码添加小易进行咨询吧',
    //           showCancel: false,
    //         })
    //       },
    //       fail: function (err) {
    //         if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
    //           // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
    //           wx.showModal({
    //             title: '提示',
    //             content: '需要您授权保存相册',
    //             showCancel: false,
    //             success: modalSuccess => {
    //               wx.openSetting({
    //                 success(settingdata) {
    //                   console.log("settingdata", settingdata)
    //                   if (settingdata.authSetting['scope.writePhotosAlbum']) {
    //                     wx.showModal({
    //                       title: '提示',
    //                       content: '获取权限成功,再次点击图片即可保存',
    //                       showCancel: false,
    //                     })
    //                   } else {
    //                     wx.showModal({
    //                       title: '提示',
    //                       content: '获取权限失败，将无法保存到相册哦~',
    //                       showCancel: false,
    //                     })
    //                   }
    //                 },
    //                 fail(failData) {
    //                   console.log("failData", failData)
    //                 },
    //                 complete(finishData) {
    //                   console.log("finishData", finishData)
    //                 }
    //               })
    //             }
    //           })
    //         }
    //       },
    //       complete(res) {
    //         wx.hideLoading()
    //       }
    //     })
    //   }
    // })
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


})